/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.model.bean;

import com.entites.Employee;
import com.entites.EventLibrary;
import com.entites.NotificationType;
import com.entites.Notifications;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Chung
 */
@Stateless
public class NotificationTypeFacade extends AbstractFacade<NotificationType> implements NotificationTypeFacadeLocal {

    @PersistenceContext(unitName = "BluePumpkinCompanyPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public NotificationTypeFacade() {
        super(NotificationType.class);
    }

    @Override
    public List<Notifications> getNotifications(int notificationId, String username) {
        List<Object[]> objArr;
        List<Notifications> listNotification = new ArrayList<>();
        NotificationType notificationType = new NotificationType();
       
        EventLibrary eventLibrary = new EventLibrary();

        Query query = em.createNamedStoredProcedureQuery("NotificationType.getNotification");
        query.setParameter("NotificationTypeID", notificationId);
        query.setParameter("Username", username);
        objArr = query.getResultList();
        for (Object[] row : objArr) {
            Notifications notifications = new Notifications();
            notifications.setId(row[0] == null ? 0 : (Integer) row[0]);
            notificationType.setId(row[1] == null ? 0 : (Integer) row[1]);
            notifications.setNotificationTypeID(notificationType);
            Employee employee = new Employee();
            employee.setUsername(row[2] == null ? "" : (String) row[2]);
            notifications.setUsernameME(employee);
             Employee employee1 = new Employee();
            employee1.setUsername(row[3] == null ? "" : (String) row[3]);
            notifications.setUsernameFE(employee1);
            eventLibrary.setEventId(row[4] == null ? 0 : (Integer) row[4]);
            notifications.setEventId(eventLibrary);
            notifications.setStatus(row[5] == null ? "" : (String) row[5]);
            notifications.setAvatarImg(row[6] == null ? "" : (String) row[6]);
            notifications.setFullname(row[7] == null ? "" : (String) row[7]);
            notifications.setMessage(row[8] == null ? "" : (String) row[8]);
            notifications.setNameEvent(row[9] == null ? "" : (String) row[9]);
            notifications.setBirthdate(row[10] == null ? new Date() : (Date) row[10]);

            listNotification.add(notifications);
        }
        return listNotification;
    }

    @Override
    public void checkNotification() {
        Query query = em.createNamedStoredProcedureQuery("NotificationType.checkNotification");
        query.executeUpdate();
    }

    @Override
    public void checkNotification1() {
        Query query = em.createNamedStoredProcedureQuery("NotificationType.checkNotification1");
        query.executeUpdate();
    }

    @Override
    public int addBirthdaywishes(Notifications notifications) {
        Query query = em.createNamedStoredProcedureQuery("NotificationType.Birthdaywishes");
        query.setParameter("UsernameFE", notifications.getUsernameFE().getUsername());
        query.setParameter("AvatarImg", notifications.getAvatarImg());
        query.setParameter("Fullname", notifications.getFullname());
        query.setParameter("Message", notifications.getMessage());
        return query.executeUpdate();
    }

    @Override
    public int addEventinvitations(Notifications notifications) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public int changeStatus(int Id) {
        Query query = em.createNamedStoredProcedureQuery("NotificationType.ChangeStatus");
        query.setParameter("Id", Id);
        return query.executeUpdate();
    }

    @Override
    public void sendInvitedAttendEvent(String usernameMe, String usernameFe, String message, int eventId) {
        try {
            Query query=em.createNamedStoredProcedureQuery("Notifications.SendInvitedAttendEvent");
                  query.setParameter("usernameME", usernameMe);
                  query.setParameter("usernameFE", usernameFe);
                  query.setParameter("messages", message);
                  query.setParameter("eventId", eventId);
                  query.executeUpdate();
            
        } catch (Exception e) {
            System.out.println("Error"+e.getMessage());
        }
    }

}
